package com.nft.qz.datasource.domain.entity;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 *
 * @author Mark sunlightcs@gmail.com
 */
public class PageUtils<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 当前页数
     */
    private long current;

    /**
     * 每页记录数
     */
    private long size;

    /**
     * 总记录数
     */
    private long total;

    /**
     * 总页数
     */
    private long pages;

    /**
     * 列表数据
     */
    private List<T> records = new ArrayList<>();

    public static PageUtils getEmptyInstance() {
        return new PageUtils();
    }

    private PageUtils() { }

    /**
     * 封装分页
     *
     * @param list       列表数据
     * @param current   当前页数
     * @param size      每页记录数
     */
    public PageUtils(List<T> list, int current, int size) {
        this.records = list;
        this.current = current;
        this.size = size;
    }

    /**
     * 封装分页
     *
     * @param list       列表数据
     * @param totalCount 总记录数
     * @param pageSize   每页记录数
     * @param currPage   当前页数
     */
    public PageUtils(List<T> list, int totalCount, int pageSize, int currPage) {
        this.current = currPage;
        this.size = pageSize;
        this.total = totalCount;
        this.pages = (long) Math.ceil((double) totalCount / pageSize);
        this.records = list;
    }

    /**
     * 封装分页
     */
    public PageUtils(IPage<T> page) {
        this.current = page.getCurrent();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.pages = page.getPages();
        this.records = page.getRecords();
    }

    /**
     * 封装分页
     */
    public PageUtils(IPage<?> page, List<T> newRecords) {
        this.current = page.getCurrent();
        this.size = page.getSize();
        this.total = page.getTotal();
        this.pages = page.getPages();
        this.records = newRecords;
    }


    public long getCurrent() {
        return current;
    }

    public void setCurrent(long current) {
        this.current = current;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public long getPages() {
        return pages;
    }

    public void setPages(long pages) {
        this.pages = pages;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    //将list转page的方法
    public static <T> PageUtils<T> ListToPage(Integer current, Integer size, List<T> list){
        PageUtils<T> pageBean = new PageUtils<T>(); //页面对象

        pageBean.setCurrent(current);  //封装当前页数

        pageBean.setSize(size);//每页显示的数据数目

        //封装总记录数
        int totalCount = list.size();
        pageBean.setTotal(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num=Math.ceil(tc/size);//向上取整
        pageBean.setPages(num.intValue());

        //封装该页list
        int start=(current-1)*size;
        if (start + size > totalCount)
            pageBean.setRecords(list.subList(start, totalCount));
        else
            pageBean.setRecords(list.subList(start, start + size));
        return pageBean;
    }
}
